package com.cskaoyan.service;



import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.domain.*;

import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author stone
 * @date 2021/11/29 15:17
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserMapper userMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FootPrintMapper footPrintMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    FeedBackMapper feedBackMapper;


    @Override
    public BaseData list(String username, String mobile,BaseParam param) {

        PageHelper.startPage(param.getPage(), param.getLimit());
        UserExample example = new UserExample();
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
       UserExample.Criteria criteria = example.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }

        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }


        
        List<User> users = userMapper.selectByExample(example);

        PageInfo info = new PageInfo(users);
        Integer total = (int)info.getTotal();
        //total == users.size() 👉 false; 如果total小于等于limit是true
        //total ≥ users.size()
        Integer pages = info.getPages();


        return BaseData.getBack(param.getLimit(),param.getPage(),pages,total,users);
    }

    @Override
    public User queryId(Integer id) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (id!=null){
            criteria.andIdEqualTo(id);
        }
        List<User> users = userMapper.selectByExample(example);
        return users.get(0);
    }


    @Override
    public BaseData addresslist(String name, Integer userId, BaseParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        AddressExample addressExample=new AddressExample();
        addressExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (name != null && !"".equals(name)) {
           criteria.andNameLike("%" + name +"%");
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        List<Address> addresses=addressMapper.selectByExample(addressExample);
        PageInfo pageInfo=new PageInfo(addresses);
        Integer total = (int)pageInfo.getTotal();
        Integer pages = pageInfo.getPages();


        return  BaseData.getBack(param.getLimit(),param.getPage(),pages,total,addresses);
    }

    @Override
    public BaseData queryCollects(Integer userId, Integer valueId, BaseParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        PageInfo info = new PageInfo(collects);
        Integer total = (int) info.getTotal();
        Integer pages = info.getPages();

        BaseData baseData = new BaseData();

        baseData.setLimit(param.getLimit());
        baseData.setPage(param.getPage());
        baseData.setPages(pages);
        baseData.setTotal(total);
        baseData.setList(collects);

        //return BaseData.getBack(param.getLimit(), param.getPage(), pages, total, collects);
        return baseData;
    }


    @Override
    public BaseData queryfootprint(Integer userId, Integer goodsId, BaseParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        FootPrintExample footPrintExample = new FootPrintExample();
        footPrintExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        FootPrintExample.Criteria criteria = footPrintExample.createCriteria();
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId!=null){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        List<FootPrint> footPrints=footPrintMapper.selectByExample(footPrintExample);
        PageInfo info=new PageInfo(footPrints);
          Integer total= (int)info.getTotal();
        Integer pages = info.getPages();

        return BaseData.getBack(param.getLimit(),param.getPage(),pages,total,footPrints);
    }

    @Override
    public BaseData queryHistories(Integer userId, String keyword, BaseParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
         searchHistoryExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(keyword!=null){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        List<SearchHistory> searchHistories= searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo info=new PageInfo(searchHistories);
        Integer total=(int)info.getTotal();
        Integer pages = info.getPages();
        return BaseData.getBack(param.getLimit(),param.getPage(),pages,total,searchHistories);
    }

    @Override
    public BaseData queryFeedbacks(String username, Integer id, BaseParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        FeedBackExample feedBackExample = new FeedBackExample();
        feedBackExample.setOrderByClause(param.getSort() + " "+ param.getOrder());
        FeedBackExample.Criteria criteria = feedBackExample.createCriteria();
        if(username != null && username !=""){
            criteria.andUsernameLike("%"+username+"%");
        }
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        List<FeedBack> feedBacks =feedBackMapper.selectByExample(feedBackExample);
        PageInfo info = new PageInfo(feedBacks);
        Integer total = (int)info.getTotal();
        Integer pages = info.getPages();
        return BaseData.getBack(param.getLimit(),param.getPage(),pages,total,feedBacks);


    }

    @Override
    public void updateData(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        userMapper.updateByExample(user,userExample);

    }


}
